#sobel算子 laplaican算子 #Canny边缘检测
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img=cv.imread("F:\\11\\16.png")
img1=cv.imread("F:\\11\\16.png",0)
def show(img,img1):
    plt.figure(figsize=(10,8),dpi=100)
    plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('origin')
    plt.xticks([]),plt.yticks([])
    plt.subplot(122),plt.imshow(img1[:,:,::-1]),plt.title('after')
    plt.xticks([]),plt.yticks([])
    plt.show()
def sobel(ksize):#sobel算子函数
    x=cv.Sobel(img,cv.CV_16S,1,0,ksize=ksize);#ksize=-1时使用的时scharr算子
    y=cv.Sobel(img,cv.CV_16S,0,1,ksize=ksize);
    Scale_absX=cv.convertScaleAbs(x);
    Scale_absY=cv.convertScaleAbs(y);
    result=cv.addWeighted(Scale_absX,0.5,Scale_absY,0.5,0);
    return result;
def laplacian():#laplacian算子函数
    result=cv.Laplacian(img,cv.CV_16S)
    Scale_abs=cv.convertScaleAbs(result)
    return Scale_abs;
def Canny():
    lowThreshold=0;
    max_lowThreshold=100;
    canny=cv.Canny(img1,lowThreshold,max_lowThreshold)
    return canny;
def show1(img,img1):#利用matplotlib显示图像
    plt.figure(figsize=(10, 8), dpi=100)
    plt.subplot(121), plt.imshow(img,cmap=plt.cm.gray), plt.title('origin')
    plt.xticks([]), plt.yticks([])
    plt.subplot(122), plt.imshow(img1,cmap=plt.cm.gray), plt.title('after')
    plt.xticks([]), plt.yticks([])
    plt.show()
if __name__=="__main__":
    show(img,sobel(-1))
    show(img,laplacian())
    show1(img1,Canny())